package com.example.fms.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.fms.dto.req.UserReqDto;
import com.example.fms.dto.resp.LoginRespDto;
import com.example.fms.dto.resp.MinOrderRuleRespDto;
import com.example.fms.dto.resp.UserRespDto;
import com.example.fms.entity.MinOrderRule;
import com.example.fms.entity.User;
import com.example.fms.entity.UserRole;
import com.example.fms.exception.BusinessException;
import com.example.fms.exception.ExceptionEnum;
import com.example.fms.mapper.StandardFreightRuleMapper;
import com.example.fms.mapper.UserMapper;
import com.example.fms.mapper.UserRoleMapper;
import com.example.fms.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.DigestUtils;

import javax.annotation.Resource;
import java.lang.reflect.InvocationTargetException;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Objects;

@Service
@Slf4j
public class UserServiceImpl  extends ServiceImpl<UserMapper, User> implements UserService {
    @Resource
    UserMapper userMapper;
    @Resource
    UserRoleMapper userRoleMapper;




    @Override
    public Page<UserRespDto> getUserList(UserReqDto req) {
        Page<UserRespDto> page = new Page<>(req.getPageNum(), req.getPageSize());
        List<UserRespDto> userList = userMapper.getUserList(page, req);
        userList.forEach(s->{
            s.setMg_state(s.getMgState());
        });
        page.setRecords(userList);
        return page;
    }

    @Override
    public Boolean save(UserReqDto req){
        //将req的内容赋值给user
        User user = new User();
        user.setStatus(1);
        user.setType("1");
        String password = req.getPassword();
        String userName = req.getUsername();
        if (StrUtil.isBlank(password)) {
            throw new BusinessException(ExceptionEnum.PASSWORD_IS_NULL);
        }
        if (StrUtil.isBlank(userName)) {
            throw new BusinessException(ExceptionEnum.USER_IS_NULL);
        }
        LambdaQueryWrapper<User> qw = new LambdaQueryWrapper<>();
        qw.eq(User::getUserName, userName);
        User userOne = userMapper.selectOne(qw);
        if (!Objects.isNull(userOne)) {
            throw new BusinessException(ExceptionEnum.USER_NOT);
        }
        //密码比对
        //MD5加密
        String md5Password = DigestUtils.md5DigestAsHex(req.getPassword().getBytes(StandardCharsets.UTF_8));
        user.setPassword(md5Password);
        user.setUserName(req.getUsername());
        user.setName(req.getName());
        user.setEmail(req.getEmail());
        user.setMobile(req.getMobile());
        userMapper.insert(user);

        return true;
    }

    @Override
    public User putType(String uId, Boolean type) {
        User user = userMapper.selectById(uId);
        if (type){
            user.setStatus(1);
        } else {
            user.setStatus(0);
        }
        userMapper.updateById(user);
        return user;
    }



    @Override
    public User putUser(Long id, UserReqDto req) {
        User user = userMapper.selectById(id);
        user.setMobile(req.getMobile());
        user.setEmail(req.getEmail());
        userMapper.updateById(user);
        return user;

    }

    @Override
    public UserRespDto getUserById(Long id) {
        UserRespDto userRespDto = new UserRespDto();
        User user = userMapper.selectById(id);
        userRespDto.setUsername(user.getUserName());
        LambdaQueryWrapper<UserRole> qw = new LambdaQueryWrapper<>();
        qw.eq(UserRole::getUserId, user.getId());
        UserRole userRole = userRoleMapper.selectOne(qw);
        userRespDto.setId(user.getId());
        if (!Objects.isNull(userRole)) {
            userRespDto.setRoleId(userRole.getRoleId());
        }
        userRespDto.setMobile(user.getMobile());
        userRespDto.setEmail(user.getEmail());
        return userRespDto;
    }

    @Override
    public Boolean deleteUserById(Long id) {
        userMapper.deleteById(id);
        LambdaQueryWrapper<UserRole> qw = new LambdaQueryWrapper<>();
        qw.eq(UserRole::getUserId, id);
        userRoleMapper.delete(qw);
        return Boolean.TRUE;
    }

    @Override
    public Boolean updateRoleByid(Long id, UserReqDto req) {
        LambdaQueryWrapper<UserRole> qw = new LambdaQueryWrapper<>();
        qw.eq(UserRole::getUserId, id);
        userRoleMapper.delete(qw);

        UserRole userRole = new UserRole();
        userRole.setUserId(id);
        userRole.setRoleId(req.getRid());
        userRoleMapper.insert(userRole);
        return Boolean.TRUE;
    }

    @Override
    public User login(UserRespDto req) {
        String userName = req.getUsername();
        String password = req.getPassword();

        LambdaQueryWrapper<User> userNameQw = new LambdaQueryWrapper<>();
        userNameQw.eq(User::getUserName, userName);
        User users = userMapper.selectOne(userNameQw);
        if (!Objects.isNull(users)) {
            String s = DigestUtils.md5DigestAsHex(password.getBytes(StandardCharsets.UTF_8));
            if (!s.equals(users.getPassword())) {
                //密码错误
                throw new BusinessException(ExceptionEnum.PASSWORD_ERROR);
            }

        } else {
            throw new BusinessException(ExceptionEnum.USER_DOES_NOT_EXIST);
        }
        return users;
    }
}
